package controllers

import (
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// MiniCityController 城市控制器
type MiniCityController struct {
	web.Controller
}

// GetCityList 获取城市列表
func (c *MiniCityController) GetCityList() {
	// 获取请求参数
	province := c.GetString("province", "")

	// 构建过滤条件
	filters := make(map[string]interface{})
	filters["status"] = 1 // 只返回启用的城市

	if province != "" {
		filters["province"] = province
	}

	// 查询所有城市
	cities, err := models.GetAllCities(filters)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取城市列表失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 构建城市列表数据
	cityList := make([]map[string]interface{}, 0)
	for _, city := range cities {
		cityData := map[string]interface{}{
			"id":       city.Id,
			"name":     city.Name,
			"code":     city.Code,
			"province": city.Province,
		}
		cityList = append(cityList, cityData)
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "获取城市列表成功",
		"data": map[string]interface{}{
			"list": cityList,
		},
	}
	c.ServeJSON()
}

// GetHotCities 获取热门城市
func (c *MiniCityController) GetHotCities() {
	// 构建过滤条件
	filters := make(map[string]interface{})
	filters["status"] = 1 // 只返回启用的城市
	filters["is_hot"] = 1 // 只返回热门城市

	// 查询热门城市
	cities, err := models.GetAllCities(filters)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取热门城市失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 构建城市列表数据
	cityList := make([]map[string]interface{}, 0)
	for _, city := range cities {
		cityData := map[string]interface{}{
			"id":       city.Id,
			"name":     city.Name,
			"code":     city.Code,
			"province": city.Province,
		}
		cityList = append(cityList, cityData)
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "获取热门城市成功",
		"data": map[string]interface{}{
			"list": cityList,
		},
	}
	c.ServeJSON()
}
